//
//  ASSeetingController.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASSeetingController.h"
#import "ASSeetingCell.h"
#import "ASUserCenterModel.h"
#import "ASWebController.h"
#import "ASFeedBackController.h"

@interface ASSeetingController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArr;

@end

@implementation ASSeetingController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.title = @"设置";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self createData];
    // Do any additional setup after loading the view.
}

- (void)createData{
    self.dataArr = [NSMutableArray new];
    NSArray *titleArr = @[@"意见反馈",/*@"给个好评呗",*/@"清除缓存",@"用户协议",@"隐私政策",@"当前版本"];
    for (int x = 0; x < titleArr.count; x++) {
        ASUserCenterModel *model = [ASUserCenterModel new];
        model.titleStr = titleArr[x];
        if ([model.titleStr containsString:@"版本"]) {
            model.hiddenArrow = YES;
            model.contentStr = [NSString stringWithFormat:@"V%@",[ASTool versionNumber]];
        }else if ([model.titleStr containsString:@"缓存"]) {
            model.contentStr = [NSString stringWithFormat:@"%.2fM",[ASTool getCacheSize]];;
        }
        [self.dataArr addObject:model];
    }
    [self.tableView reloadData];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = self.view.backgroundColor;
        _tableView.alwaysBounceVertical = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ASSeetingCell class] forCellReuseIdentifier:ASSeetingCellStr];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ASSeetingCell *cell = [tableView dequeueReusableCellWithIdentifier:ASSeetingCellStr];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.dataArr.count > indexPath.row) {
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > indexPath.row) {
        ASUserCenterModel * model = self.dataArr[indexPath.row];
        if ([model.titleStr containsString:@"意见"]) {
            [self pushFeedBackVC];
        }else if ([model.titleStr containsString:@"用户协议"]) {
            [self pushWebProtocolIfPrivacy:YES];
        }else if ([model.titleStr containsString:@"隐私政策"]) {
            [self pushWebProtocolIfPrivacy:NO];
        }else if ([model.titleStr containsString:@"好评"]) {
            [self pushAppStore];
        }else if ([model.titleStr containsString:@"缓存"]) {
            [self cleanCatch:model indexPath:indexPath];
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
}

///跳转意见反馈页面
- (void)pushFeedBackVC{
    [ASTool addUMTongJiKey:@"user_center" subKey:@"Setup" titleStr:@"意见反馈"];
    ASFeedBackController *feedVC = [ASFeedBackController new];
    [self.navigationController pushViewController:feedVC animated:YES];
}

///跳转App Store评价页面
- (void)pushAppStore{
    [ASTool addUMTongJiKey:@"user_center" subKey:@"Setup" titleStr:@"给个好评"];
    NSString *appURL = @"https://itunes.apple.com/cn/app/id1533365227?action=write-review";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL] options:@{} completionHandler:^(BOOL success) {
    }];
}

///清理缓存
- (void)cleanCatch:(ASUserCenterModel *)model indexPath:(NSIndexPath *)indexPath{
    [ASTool clearFile];
    CGFloat size = [ASTool getCacheSize];
    model.contentStr = [NSString stringWithFormat:@"%.2fM",size];
    [self.tableView reloadRow:indexPath.row inSection:indexPath.section withRowAnimation:UITableViewRowAnimationFade];
    [MBProgressHUD showSuccess:@"清除成功"];
}

///跳转web评价页面
- (void)pushWebProtocolIfPrivacy:(BOOL)IfPrivacy{
    ASWebController *webVC = [ASWebController new];
    webVC.hidesBottomBarWhenPushed = YES;
    if(IfPrivacy){
        webVC.loadUrl = WEB_PROTOCOL_URL;
        webVC.title = @"用户协议";
    }else{
        webVC.loadUrl = WEB_PRIVACY_URL;
        webVC.title = @"隐私政策";
    }
    [self.navigationController pushViewController:webVC animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
